package com.example.lancooznbk.presenter;

import android.util.Log;

import com.example.lancooznbk.model.api.RecentStudyApi;
import com.example.lancooznbk.model.result.RecentStudyItemEntity;
import com.example.lancooznbk.presenter.presenter_interface.IRecentStudyPresenter;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import java.util.List;

import znbkFragment.ZnbkMainFragment;

/**
 * Created by Administrator on 2018\2\6 0006.
 */

public class RecentStudyPresenter implements IRecentStudyPresenter {
    private ILoadMoreDataView mView;
    private String IsRun = "1";
    private int MonthCount = 5;
    private int WeekCount = 7;

    //    private int wrongFrom;
    private ProgressSubscriber mSubscriber;
    private HttpOnNextListener<RecentStudyItemEntity> httpOnNextListener = new HttpOnNextListener<RecentStudyItemEntity>() {
        @Override
        public void onNext(RecentStudyItemEntity recentStudyItemEntity) {
//            if (!recentStudyItemEntity.getPredictInfo().getPredictDate().equals("")) {
////                mView.hideLoading();
//                List<RecentStudyItemEntity.LastAnswerBean> lastAnswerList = recentStudyItemEntity.getLastAnswer();
//                for (int i = 0 ; i < lastAnswerList.size() ; i ++){ //预处理题库返回不合理的地方
//                    String summary = lastAnswerList.get(i).getTitle();
//                    /*summary = TagHelperUtils.replacAllTag(summary,"<P>","");
//                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
//                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
//                    summary = TagHelperUtils.replaceImageTag(summary);
//                    if (summary.length() >100 &&
//                            !(summary.contains("<table") || summary.contains("<TABLE"))){
//                        summary = summary.substring(0,100);
//                    }*/
//
//                    String regexStr = "<[^>]*>"; //去除所有的标签
//                    summary = summary.replaceAll(regexStr, "");
//                    if (summary.length() > 100){
//                        summary = summary.substring(0,100);
//                    }
//                    lastAnswerList.get(i).setTitle(summary);
//                }
//                    mView.loadData(recentStudyItemEntity);
//            }else {
//                mView.showNoData("获取失败");
//            }
            mView.loadData(recentStudyItemEntity);
        }


        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
        }
    };

    public RecentStudyPresenter(ILoadMoreDataView mView) {
        this.mView = mView;
//        this.wrongFrom = wrongFrom;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        RecentStudyApi api = new RecentStudyApi(httpOnNextListener,(ZnbkMainFragment)mView);

        api.setIsRun(IsRun);
        api.setMonthCount(MonthCount);
        api.setWeekCount(WeekCount);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }
    @Override
    public void getData(boolean isShowing) {
//        if (isShowing) mView.showLoading();
        getData();
    }

    @Override
    public void getMoreData() {
        getData();
    }

    @Override
    public void getDataByStatus(int quesStatusId) {
           getData();
    }
}
